package com.mysterymall.Controller;

import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Comp.LocalCache;
import com.mysterymall.Common.Comp.Log;
import com.mysterymall.Common.Utils.BaseEnum;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Model.Dto.OrderDto;
import com.mysterymall.Model.Dto.WithdrawDepositQueryDto;
import com.mysterymall.Model.Entity.OrderInfo;
import com.mysterymall.Model.Entity.WithdrawDeposit;
import com.mysterymall.Service.OrderInfoService;
import com.mysterymall.Service.PaymentService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @ClassName com.mysterymall.Controller
 * @Description
 * @Author HuangWX
 * @Time 2023/12/02  16:17
 */
@RestController
@RequestMapping("/order")
public class OrderController {


    private final OrderInfoService orderInfoService;


    private final PaymentService paymentService;



    /**
     * 构造器注入
     * @param orderInfoService 订单服务
     * @param paymentService 支付服务
     */
    public OrderController(OrderInfoService orderInfoService, PaymentService paymentService) {
        this.orderInfoService = orderInfoService;
        this.paymentService = paymentService;
    }

    /**
     * 查询我的订单
     * @param type 订单类型
     * @param currIndex 当前页
     * @param pageSize 每页条数
     * @return Result
     */
    @GetMapping("/getMyOrderList")
    @Log
    public Result getMyOrderList(@RequestParam("type")String type, @RequestParam("currIndex")Integer currIndex, @RequestParam("pageSize") Integer pageSize,
                                 HttpServletRequest request){
        return orderInfoService.getMyAllOrder(type,currIndex,pageSize,request);

    }

    /**
     * 查询订单详情
     * @param orderCode 订单编号
     * @return Result
     */
    @GetMapping("/getOrderInfo")
    @Log
    public Result getOrderInfo(@RequestParam("orderCode")String orderCode, HttpServletRequest request){
        return orderInfoService.getOrderInfo(orderCode,request);
    }

    /**
     * 充值游戏币
     * @return Result
     */
    @PostMapping("/topUpCoin")
    @Log
    public Result topUpCoin(@RequestBody OrderDto orderDto) throws Exception {

        if(orderDto.getPaymentClassId() == null || orderDto.getPaymentClassId().isEmpty()){
            if(orderDto.getAmount().compareTo(new BigDecimal(30)) < 0){
                return new Result(HttpStatus.BAD_NOT_PASS,"message.orderCont.orderError");
            }
        }
        Map<String, Object> stringObjectMap = orderInfoService.insertOrder(orderDto);

        if(stringObjectMap.get("Result") != null){
            return (Result) stringObjectMap.get("Result");
        }else{
            OrderInfo orderInfo = (OrderInfo) stringObjectMap.get("orderInfo");
            return paymentService.placeAnOrder(orderInfo);
        }
    }

    // 购买商品
    @PostMapping("/buyGoods")
    @Log
    public Result buyGoods(@RequestBody OrderDto orderDto) throws Exception {

        if(orderDto.getGoodsType() == null || orderDto.getGoodsType().isEmpty()){
            return new Result(HttpStatus.BAD_NOT_PASS,"message.orderCont.orderError");
        }

        Map<String, Object> stringObjectMap = orderInfoService.insertOrder(orderDto);

        if(stringObjectMap.get("Result") != null){
            return (Result) stringObjectMap.get("Result");
        }else{
            OrderInfo orderInfo = (OrderInfo) stringObjectMap.get("orderInfo");
            return paymentService.placeAnOrder(orderInfo);
        }
    }


    /**
     * 游戏币支付
     * @param orderDto 订单信息
     * @return Result
     */
    @PostMapping("/luckyCoinPayment")
    @Log
    public Result luckyCoinPayment(@RequestBody OrderDto orderDto){

        if(orderDto.getGoodsType() == null || orderDto.getGoodsType().isEmpty()){
            return new Result(HttpStatus.BAD_NOT_PASS,"");
        }

        Map<String, Object> stringObjectMap = orderInfoService.insertOrder(orderDto);

        if(stringObjectMap.get("Result") != null){
            return (Result) stringObjectMap.get("Result");
        }else{
            OrderInfo orderInfo = (OrderInfo) stringObjectMap.get("orderInfo");
            return paymentService.luckyCoinPayment(orderInfo);
        }
    }


    // 查询支付结果
    @GetMapping("/getPaymentResult")
    @Log
    public Result getPaymentResult(@RequestParam("orderId")String orderId){

        // 从缓存中获取支付结果
        if(LocalCache.CACHE.get("PaymentResult-"+orderId) != null){
            int x = (int) LocalCache.CACHE.get("PaymentResult-"+orderId);
            if(x >= 10){
                return new Result(HttpStatus.BAD_NOT_PASS,"message.orderCont.orderError");
            }else{
                LocalCache.CACHE.put("PaymentResult-"+orderId,x+1);
                return paymentService.getPaymentResult(orderId);
            }
        }else{
            LocalCache.CACHE.put("PaymentResult-"+orderId,1);
            return paymentService.getPaymentResult(orderId);
        }
    }


    /**
     * 提货
     * @param goodsId 商品id
     * @return OrderInfo
     */
    @GetMapping("/pickUpGoods")
    @Log
    public Result pickUpGoods(@RequestParam("goodsId")String goodsId ,@RequestParam("addressId")String addressId){
        return orderInfoService.pickUpGoods(goodsId,addressId);
    }


    /**
     * 申请提现
     * @param  amount 金额
     * @return Result
     */
    @GetMapping("/applyForWithdrawal")
    @Log
    public Result applyForWithdrawal(@RequestParam("amount") BigDecimal amount){
        return orderInfoService.applyForWithdrawal(amount);
    }


    /**
     * 查询提现申请列表
     * @param currIndex 当前页
     * @param pageSize 每页条数
     * @param status 状态
     * @return Result
     */
    @GetMapping("/getMyWithdrawDepositList")
    @Log
    public Result getMyWithdrawDepositList(@RequestParam("currIndex")Integer currIndex, @RequestParam("pageSize") Integer pageSize,
                                          @RequestParam("status") String status){

        List<WithdrawDeposit> withdrawDepositList = orderInfoService.getMyWithdrawDepositList(currIndex,pageSize,status);

        for (WithdrawDeposit withdrawDeposit : withdrawDepositList) {
            if(withdrawDeposit.getWithdrawStatus().equals(BaseEnum.Account_Reached.getCode()) || withdrawDeposit.getWithdrawStatus().equals(BaseEnum.Account_Failed.getCode()) ||
                    withdrawDeposit.getWithdrawStatus().equals(BaseEnum.Audit_Not_Passed.getCode())){
                continue;
            }
            WithdrawDepositQueryDto withdrawDepositQueryDto =  paymentService.withdrawDepositQuery(withdrawDeposit);
            if(withdrawDepositQueryDto != null){
                switch (withdrawDepositQueryDto.getResult()){
                    case 1: {
                        withdrawDeposit.setWithdrawStatus(BaseEnum.Account_Reached.getCode());
                        orderInfoService.updateWithdrawDepositStatus(withdrawDeposit);
                        break;
                    }
                    case 2: {
                        withdrawDeposit.setWithdrawStatus(BaseEnum.Account_Failed.getCode());
                        orderInfoService.updateWithdrawDepositStatus(withdrawDeposit);
                        break;
                    }
                }
            }
        }
        withdrawDepositList = orderInfoService.getMyWithdrawDepositList(currIndex,pageSize,status);
        return new Result(HttpStatus.SUCCESS,"message.querySuccess",withdrawDepositList);
    }
}
